package com.tabjin.rfidsocket.common.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author tabjin
 * create at 2020/10/27 17:46
 * @program food
 * @description
 *              自定义响应数据结构
 * 				本类可提供给 H5/ios/安卓/公众号/小程序 使用
 * 				前端接受此类数据（json object)后，可自行根据业务去实现相关功能
 *
 * 				200：表示成功
 * 				500：表示错误，错误信息在message字段中
 * 				501：bean验证错误，不管多少个错误都以map形式返回
 * 				502：拦截器拦截到用户token出错
 * 				555：异常抛出信息
 * 				556: 用户qq校验异常
 */
public class TabJsonResult {
    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object data;

    @JsonIgnore
    private String ok;	// 不使用

    public static TabJsonResult build(Integer status, String message, Object data) {
        return new TabJsonResult(status, message, data);
    }

    public static TabJsonResult build(Integer status, String message, Object data, String ok) {
        return new TabJsonResult(status, message, data, ok);
    }

    public static TabJsonResult ok(Object data) {
        return new TabJsonResult(data);
    }

    public static TabJsonResult ok() {
        return new TabJsonResult(null);
    }

    public static TabJsonResult errorMessage(String message) {
        return new TabJsonResult(500, message, null);
    }

    public static TabJsonResult errorMap(Object data) {
        return new TabJsonResult(501, "error", data);
    }

    public static TabJsonResult errorTokenMessage(String message) {
        return new TabJsonResult(502, message, null);
    }

    public static TabJsonResult errorException(String message) {
        return new TabJsonResult(555, message, null);
    }

    public static TabJsonResult errorUserQQ(String message) {
        return new TabJsonResult(556, message, null);
    }

    public TabJsonResult() {

    }

    public TabJsonResult(Integer status, String message, Object data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public TabJsonResult(Integer status, String message, Object data, String ok) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.ok = ok;
    }

    public TabJsonResult(Object data) {
        this.status = 200;
        this.message = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == 200;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getmessage() {
        return message;
    }

    public void setmessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }
}
